import { createStore } from 'redux'
import { SHOW_TOAST, HIDE_TOAST } from './ActionType';

const defState: IRootState = {
    toastData: {
        show: false,
        type: "success",
        message: 'ok'
    }
}

const reducer = (state = defState, action: IRootAction) => {
    // 面试题的网站里面有这个用法
    state = JSON.parse(JSON.stringify(state))
    // 准备一个显示 toast 的action
    switch (action.type) {
        case SHOW_TOAST:
            const payload = action.payload as IToastData;
            // 把 toast 显示
            // 修改内容和类型
            state.toastData = payload;
            break;
        case HIDE_TOAST:
            state.toastData.show = false;
            break;
    }
    return state
}

const store = createStore(reducer)

export default store;